package com.comdo.newup_loan.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.comdo.newup_loan.MyApplication;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.module.login.LoginActivity;

import java.math.BigDecimal;

public class CommonUtil {

	@SuppressLint("NewApi")
	public static void copy(Context context, String content) {
		ClipboardManager clipboard = (ClipboardManager) context
				.getSystemService(Context.CLIPBOARD_SERVICE);
		ClipData clip = ClipData.newPlainText("label", content);
		clipboard.setPrimaryClip(clip);
	}

	public static void calcViewMeasure(View view) {
		int width = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int expandSpec = View.MeasureSpec.makeMeasureSpec(
				Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
		view.measure(width, expandSpec);
	}

	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public static float rounding(float value) {
		BigDecimal decimal = new BigDecimal(value);
		return decimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
	}
	  /**
     * 当浮点型数据位数超过10位之后，数据变成科学计数法显示。用此方法可以使其正常显示。
     * @param value
     * @return Sting
     */
    public static String formatFloatNumber(double value) {
        if(value != 0.00){
            java.text.DecimalFormat df = new java.text.DecimalFormat("########");
            return df.format(value);
        }else{
            return "0";
        }

    }
    public static String formatFloatNumber(Double value) {
        if(value != null){
            if(value.doubleValue() != 0.00){
                java.text.DecimalFormat df = new java.text.DecimalFormat("########");
                return df.format(value.doubleValue());
            }else{
                return "0";
            }
        }
        return "";
    }
    public static void exitLogin(Activity activity){
		Config.loginInfo=null;
		ACache.get(activity).put("loginInfo", Config.loginInfo);
		Intent intent = new Intent(activity, LoginActivity.class);
		activity.startActivity(intent);
		activity.finish();
		MyApplication.getInstance().finishAll();
	}

}
